package com.jeeplus.modules.guide.utils;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImgUtils {
    public static Boolean  ImgToDo(String imgA,String imgB,String url){
        try{
            InputStream is1 = new FileInputStream(imgA);
            InputStream is2 = new FileInputStream(imgB);
            BufferedImage readImg1 = ImageIO.read(is1);
            BufferedImage readImg2 = ImageIO.read(is2);
            // 图像压缩  保证每张图片大小一致
            int width = readImg1.getWidth();
            int height = readImg1.getHeight();
            readImg2 = resize(readImg2, width, height);
            BufferedImage mergeImg = mergeImage(readImg1, readImg2);
            writeImageLocal(url, mergeImg);
            is1.close();
            is2.close();
            return true;
        }catch(IOException ex){}
        return false;
    }
    /**
     * 压缩图片
     */
    private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
        // targetW，targetH分别表示目标长和宽
        int type = source.getType();
        BufferedImage target = null;
        int width = source.getWidth();
        int height = source.getHeight();
        double sx = (double) targetW / width;
        double sy = (double) targetH / height;
        // 这里想实现在targetW，targetH范围内实现等比缩放
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
        if (type == BufferedImage.TYPE_CUSTOM) {
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW,
                    targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else {
            target = new BufferedImage(targetW, targetH, type);
        }
        Graphics2D g = target.createGraphics();
        // smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }
    /**
     * 待合并的两张张图必须满足这样的前提，如果水平方向合并，则高度必须相等；如果是垂直方向合并，宽度必须相等。
     *
     * @param img1 待合并的第一张图
     * @param img2 带合并的第二张图
     * @return 返回合并后的BufferedImage对象
     */
    private static BufferedImage mergeImage(BufferedImage img1, BufferedImage img2) {
        int w1 = img1.getWidth();
        int h1 = img1.getHeight();
        int w2 = img2.getWidth();
        Graphics2D graphics2D = null;
        // 生成新图片
        BufferedImage destImage = null;
        destImage = new BufferedImage(w1 + w2, h1, BufferedImage.TYPE_INT_RGB);
        graphics2D = destImage.createGraphics();
        graphics2D.drawImage(img1, 0, 0, w1, h1, null);
        graphics2D.drawImage(img2, w1, 0, w2, h1, null);
        graphics2D.dispose();
        return destImage;
    }
    /**
     * 生成新图片到本地
     */
    public static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static byte[] getData(String url) {
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .method("GET", null)
                    .build();
            Response response = client.newCall(request).execute();
            return response.body().bytes();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return null;
    }
}
